<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------

namespace app\manage\controller\admin;


use buwang\base\AdminBaseController;


/**
 * @ControllerAnnotation(title="应用功能表")
 */
class MiniappOrder extends AdminBaseController
{
    use \buwang\traits\Crud;

    public function initialize()
    {
        parent::initialize();
        $this->layout && $this->app->view->engine()->layout($this->layout);
        $this->model = new \app\common\model\MiniappOrder();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFieds')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->with('module')
                ->where($where)
                ->count();
            $list = $this->model
                ->with('module')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();

            if (is_iterable($list)) {
                $memberIds = [];
                foreach ($list as $detail) {
                    $memberIds[ $detail['member_id'] ] = 1;
                }
                $memberNameMap = $this->_getMemberNames(array_keys($memberIds));
                foreach ($list as &$line) {
                    if (isset($memberNameMap[ $line['member_id'] ])) {
                        $line['member_name'] = $memberNameMap[ $line['member_id'] ];
                    } else {
                        $line['member_name'] = '已删除';
                    }
                }
            }

            $data = [
                'total' => $count,
                'list' => $list,
            ];
            return $this->success('success', $data);
        }
        return view();
    }

    protected function _getMemberNames(array $ids):array {
        $records = \app\common\model\Member::whereIn('id', $ids)
            ->select();
        $map     = [];
        if (is_iterable($records)) {
            foreach ($records as $record) {
                $map[ $record->id ] = $record->nickname;
            }
        }

        return $map;
    }

}